<?php
/*
	文件下载
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	if(!$_SESSION['download']['id'] || !$_SESSION['download']['parts'] || !$_SESSION['download']['to']){
		exi('参数错误！无法下载！','error');
	}
	$back=$_SESSION['download']['back']?$_SESSION['download']['back']:'goback';
	if($_SESSION['download']['parts']==1 && false){//只有一批数据，直接下载并返回。该方法返回后会出现页面无响应的问题，因此暂不启用
		$post=array('id'=>$_SESSION['download']['id'],'part'=>1);
		$data=cloud_request('update.download',$post,true);
		if(!$data){
			exi('数据获取失败！');
		}
		file_put_contents(ROOT_AT.$_SESSION['download']['to'],$data,FILE_APPEND);
		exi('','',$back);
	}else{
		$return=array(
			'parts'=>$_SESSION['download']['parts'],
			'progress'=>1,
			'back'=>$back,
		);
		exi($return);
	}
	
}
if($_RQ['op']=='download'){
	$part=intval($_RQ['part']);
	if(!$part){
		exi('参数错误，无法下载2！','error');
	}
	$post=array('id'=>$_SESSION['download']['id'],'part'=>$part);
	$data=cloud_request('update.download',$post,true);
	if(!$data){
		exi('数据获取失败！');
	}
	if(is_dir($_SESSION['download']['to'])){
		$file=$_SESSION['download']['to'];
	}else{
		$file=ROOT_AT.$_SESSION['download']['to'];
	}
	file_put_contents($file,$data,FILE_APPEND);
	exi('success');
}
